@extends('layouts.default')

@section('title', '量表管理')

@push('scripts')
<script>
    var filters = {};
    var per_page = 10;
    var totalNum = 0;
    layui.use(['layer','form','table','laytpl','laydate','element','laypage'], function(){
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var laytpl = layui.laytpl;
        var laydate = layui.laydate;
        var element = layui.element;
        var laypage = layui.laypage;

        
        renderSelect();

        laydate.render({
            elem: '#award-date'
            ,type: 'date'
            ,range: true
        });

        // 搜索查询
        form.on('submit(queryBtn)', function(data) {
            var param = data.field;
            filters = param
            getList(1, per_page);
        });
        // 重置
        form.on('submit(resetBtn)', function(data) {
            filters = {};
            $('#qryForm')[0].reset();
            getList(1, per_page)
        });
        // 获取页面数据
        function getList(page, limit) {
            if ($.isEmptyObject(filters)){
                let param = form.val("qryForm");
                filters = param;
            }
            
            var para = {
                "page": page,
                "search": filters,
                "limit": limit,
            };

            getScaleList(para, function(res){
                if(res.code === 1) {
                    totalNum = res.total;
                    per_page = res.limit;
                    renderPageBar(page, per_page)

                    table.render({
                        data: res.data,
                        elem: '#tablelsit',
                        toolbar: '#toolbarHead', //开启头部工具栏，并为其绑定左侧模板
                        defaultToolbar: [],
                        cellMinWidth: 80, //全局定义常规单元格的最小宽度，layui 2.2.1 新增
                        limit: res.limit,
                        skin: 'rows',
                        cols: [[
                            {type: 'numbers', title: '序号'},
                            // {field: 'term', title: '学年学期', align: 'left', templet: function(d) {
                            //     return d.term.select_year;
                            // }},
                            {field: 'name', title: '量表名称', align: 'left'},
                            {field: '', title: '量表分类', align: 'left', templet: function(d) {
                                switch(d.type) {
                                    case 1: return '岗位竞聘'; break;
                                    case 2: return '年度考核'; break;
                                    case 3: return '教研活动'; break;
                                    default: return '-'; break;
                                }
                            }},
                            {field: 'opt_name', title: '创建人', align: 'left'},
                            {field: 'enable', title: '是否启用', align: 'center', templet: function(d) {
                                switch(d.enable) {
                                    case 1: return '正常'; break;
                                    case 2: return '禁用'; break;
                                    default: return '-'; break;
                                }
                            }},
                            {field: 'created_at', title: '创建时间', align: 'center'},
                            {field: 'updated_at', title: '更新时间', align: 'center'},
                            {title: '操作', width: '20%', align: 'center', toolbar: '#opDemo', fixed:'right'}
                        ]],
                        done: function(res, curr, count) {
                            // setBtnPermission();
                            curr_page = page;
                        },
                        page: false,
                        loading: true
                    });
                } else {
                    layer.msg(res.msg, {time: 2000, icon: 5});
                }
            });
        }

        // 渲染页面
        function renderPageBar(page,limit) {
            laypage.render({
                elem: 'pageBar',
                count: totalNum, //数据总数，从服务端得到
                curr: page,
                first: '第一页',
                last: '最后一页',
                limit:limit,
                layout: ['limit','count', 'prev', 'page', 'next', 'skip'],
                jump: function(obj, first) {
                    //首次不执行
                    if(!first) {
                        layer.closeAll();
                        getList(obj.curr,obj.limit)
                    }
                }
            });
        }

        //表格头工具栏事件
        table.on('toolbar(tablelsit)', function(obj){
            switch(obj.event){
                case 'add':
                    
                break;
            };
        });
        //监听行工具事件
        table.on('tool(tablelsit)', function(obj){
            var data = obj.data;

            if(obj.event === 'del'){
                layer.confirm('确定删除吗?', function(index){
                    delScale({id: data.id}, function(res) {
                        if (res.code == '9') {
                            layer.msg(res.msg, {icon: 5, shift: 6});
                        } else {
                            layer.msg('删除成功', {icon: 6,time: 1000},function(){
                                obj.del();
                                layer.close(index);
                            });
                        }
                    });
                });
            } else if(obj.event === 'copy'){
                layer.prompt({ 
                    title: '请输入考核量表名称',
                    area: ['200px', '350px'] //自定义文本域宽高
                }, function(value, index, elem){
                    copyScale({id: data.id, name: value}, function(res) {
                        if (res.code == '9') {
                            layer.msg(res.msg, {icon: 5, shift: 6});
                        } else {
                            layer.msg('复制成功', {icon: 6,time: 1000},function(){
                                layer.closeAll();
                                getList(1, per_page);
                            });
                        }
                    });
                });
            }else if(obj.event === 'edit'){
                window.location.href = "{{route('systems.scales.editView')}}?id="+data.id;
            }else if(obj.event === 'deal'){           
                    window.location.href = "{{route('systems.scales.dealView')}}?id="+data.id+'&type='+data.type;                          
            }
        });

        function renderSelect(){
            // getTermData({},function(res){
            //     if(res.code == 1){
            //         $('[data-role=termList]').empty().append('<option value="">请选择</option>');
            //         for(var i in res.data){
            //             $('[data-role=termList]').append('<option value="'+res.data[i].id+'">' + res.data[i].select_year + '</option>');
            //         }
                                    
            //         form.render('select');
            //     }
            // });  
            getList(1, per_page);
        }

    });
</script>
@endpush

@section('content')
<style>
    
</style>

<div class="layui-card">
    <div class="layui-card-body">
        <div class="layui-row">
            <form id="qryForm" class="layui-form" action="">
                <div class="layui-form-item">
                    <div class="layui-inline"> 
                        <label class="layui-form-label">学年学期：</label>
                        <div class="layui-input-inline">
                            <select name="term_id" lay-filter="termList" data-role="termList"></select>
                        </div>
                    </div>
                    <div class="layui-inline"> 
                        <label class="layui-form-label">提交人：</label>
                        <div class="layui-input-inline">
                            <input type="text" name="opt_name" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline"> 
                        <label class="layui-form-label">提交时间：</label>
                        <div class="layui-input-inline">
                            <input type="text" name="sub_date" id="sub-date" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline" >
                        <input type="text" name="keyword" class="layui-input" placeholder="输入关键字搜索" autocomplete="off">
                    </div>
                    <div class="layui-inline">
                        <button type="button" lay-submit lay-filter="queryBtn" class="layui-btn layui-btn-normal">查询</button>
                        <button type="button" lay-submit lay-filter="resetBtn" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

<div class="layui-card">
    <script type="text/html" id="toolbarHead">
        <div class="list-head">
            <h3>量表管理列表</h3>
            <div class="layui-btn-container list-opt-btns">
                <a btnPermission="" class="layui-btn layui-btn-normal" href="{{ route('systems.scales.addView') }}" id="addBtn"> + 新增 </a>
            </div>
        </div>
    </script>

    <div class="layui-card-body">
        <table class="layui-hide" id="tablelsit" lay-filter="tablelsit" lay-data="{id: 'tablelsit'}"></table>
        <div class="pagebarbox" style="text-align: end"><div id="pageBar" class="pagebar"></div></div>
    </div>

    <script type="text/html" id="opDemo">
        <a btnPermission="" class="layui-btn layui-btn-primary layui-border-orange layui-btn-xs" lay-event="copy">复制</a>
        <a btnPermission="" class="layui-btn layui-btn-primary layui-border-blue layui-btn-xs" lay-event="edit">编辑</a>
        <a btnPermission="" class="layui-btn layui-btn-primary layui-border-blue layui-btn-xs" lay-event="deal">维护</a>
        <a btnPermission="" class="layui-btn layui-btn-primary layui-btn-xs" lay-event="del">删除</a>
    </script>
</div>
  
@endsection
